<?php
namespace Admin\Controller;
use Lib\Api\OrganizationStatisticsApi;
use Think\Controller;
class OrganizationStatisticsController extends Controller {
    private $modelName = 'OrganizationStatistics';

    public function index(){

        if (!empty($_GET['date'])) {
            $this->assign('d', $_GET['date']);
        } else {
            $modelObj = M($this->modelName);
            $date = $modelObj->min('settlement_date');
            if (empty($date)) {
                $this->assign('d', date('Y-m-d'));
            } else {
                $dates = explode(' ', $date);
                $this->assign('d', $dates[0]);
            }
        }

        $this->display();
    }

    public function tolist() {
        $page_size = $_POST['page_size'];
        $page_num = $_POST['page_num'];
        $data_cycle = $_POST['data_cycle'];
        $search_date = explode('/', $data_cycle);
        $begin_date = $search_date[0];
        $end_date = $search_date[1];

        $belong_organization_id = $_POST['organization_id'];

        $organization_id = session('admin_id');

        $field = "a.*, b.name, b.code, b.p_code, b.level, e.name as role_name";

        $where["a.state"] = array("eq", 1);
        $where['DATE_FORMAT(a.settlement_date, "%Y-%m-%d")'] = array(array('egt', $begin_date), array('elt',$end_date));
        if ($belong_organization_id != '') {
            $where['a.organization_id']  = array('in', $belong_organization_id);
        } else {
            $where['_string'] = '(c.belong_organization_id='.$organization_id.' and DATE_FORMAT(c.datetime, "%Y-%m-%d %H:%i:%s") <= DATE_FORMAT("'.date('Y-m-d H:i:s').'", "%Y-%m-%d %H:%i:%s")) or a.organization_id = '.$organization_id;
        }

        $left = 'left join fs_organization b on b.id = a.organization_id';
        $left .= ' left join fs_organization_belong c on c.organization_id = b.id';
        $left .= ' left join fs_organization_role d on d.organization_id = a.organization_id';
        $left .= ' left join fs_role e on e.id = d.role_id';

        $list = OrganizationStatisticsApi::getList($where, $left, $page_num, $page_size, $field, 'a.settlement_date desc', 'a.id');

        $model = M();
        $sql = "select count(t.id) total from";
        $sql .= " (select a.id, a.state from fs_organization_statistics a left join fs_organization b on b.id = a.organization_id";
        $sql .= " left join fs_organization_belong c on c.organization_id = b.id left join fs_organization_role d on d.organization_id = a.organization_id left join fs_role e on e.id = d.role_id";
        if ($belong_organization_id != '') {
            $sql .= " where a.organization_id in (".$organization_id.") ";
        } else {
            $sql .= " where (c.belong_organization_id=".$organization_id." and DATE_FORMAT(c.datetime, \"%Y-%m-%d %H:%i:%s\") <= DATE_FORMAT('".date('Y-m-d H:i:s')."', \"%Y-%m-%d %H:%i:%s\")) or a.organization_id = ".$organization_id;
        }
		// $sql .= " where (c.belong_organization_id=".$organization_id." and DATE_FORMAT(c.datetime, \"%Y-%m-%d %H:%i:%s\") <= DATE_FORMAT('".date('Y-m-d H:i:s')."', \"%Y-%m-%d %H:%i:%s\")) or a.organization_id = ".$organization_id;
        $sql .= " group by a.id) t where t.state = 1";
        $total = $model->query($sql);

       //  $total = OrganizationStatisticsApi::getTotal($where, $left);
        $user_list = pageHelper($page_size, $page_num, $list, $total[0]["total"]);
        $rel['data'] = $user_list;

        /*$model = M();
        $sum_field = "sum(a.recharge) sum_recharge, sum(a.recharge_fee) sum_recharge_fee";
        $sum_field .= ",sum(a.withdrawals) sum_withdrawals, sum(a.withdrawals_fee) sum_withdrawals_fee";
        $sum_field .= ",sum(a.retained_money) sum_retained_money, sum(a.fee) sum_fee";
        $sum_field .= ",sum(a.cash_profit_loss) sum_cash_profit_loss, sum(a.count_profit) sum_count_profit";
        $sum_field .= ",sum(a.relation_fee) sum_relation_fee, sum(a.original_fee) sum_original_fee";
        $sum_field .= ",sum(a.turnover) sum_turnover";
        $sum = $model->table('fs_organization_statistics a')
            ->field($sum_field)
            ->join($left)
            ->where($where)
            ->select();
        $rel['sum'] = $sum[0];*/

        $model = M();
        $sql = "select";
        $sql .= " sum(t.recharge) sum_recharge, sum(t.recharge_fee) sum_recharge_fee";
        $sql .= ",sum(t.withdrawals) sum_withdrawals, sum(t.withdrawals_fee) sum_withdrawals_fee";
        $sql .= ",sum(t.retained_money) sum_retained_money, sum(t.fee) sum_fee";
        $sql .= ",sum(t.cash_profit_loss) sum_cash_profit_loss, sum(t.count_profit) sum_count_profit";
        $sql .= ",sum(t.relation_fee) sum_relation_fee, sum(t.original_fee) sum_original_fee";
        $sql .= ",sum(t.turnover) sum_turnover";
        $sql .= " from (";
        $sql .= " select a.recharge, a.recharge_fee, a.withdrawals, a.withdrawals_fee, a.retained_money, a.fee, a.cash_profit_loss, a.count_profit, a.relation_fee, a.original_fee, a.turnover, a.state ";
        $sql .= " from fs_organization_statistics a left join fs_organization b on b.id = a.organization_id";
        $sql .= " left join fs_organization_belong c on c.organization_id = b.id left join fs_organization_role d on d.organization_id = a.organization_id left join fs_role e on e.id = d.role_id";
        if ($belong_organization_id != '') {
            $sql .= " where a.organization_id in (".$organization_id.") ";
        } else {
            $sql .= " where (c.belong_organization_id=".$organization_id." and DATE_FORMAT(c.datetime, \"%Y-%m-%d %H:%i:%s\") <= DATE_FORMAT('".date('Y-m-d H:i:s')."', \"%Y-%m-%d %H:%i:%s\")) or a.organization_id = ".$organization_id;
        }
		// $sql .= " where (c.belong_organization_id=".$organization_id." and DATE_FORMAT(c.datetime, \"%Y-%m-%d %H:%i:%s\") <= DATE_FORMAT('".date('Y-m-d H:i:s')."', \"%Y-%m-%d %H:%i:%s\")) or a.organization_id = ".$organization_id;
        $sql .= " group by a.id) t where t.state = 1";
        $sum = $model->query($sql);
        $rel['sum'] = $sum[0];

        $this->ajaxReturn($rel);
    }
}